package com.ternnetwork.clustersyn.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.apache.log4j.Logger;


public class FtpServerUtil {
	
	private static final Logger log = Logger.getLogger(FtpServerUtil.class);
	
	public static void main(String[] args) {
		
	}
	
	public static void createServer(List<BaseUser> userList,Integer port){
 		   FtpServerFactory serverFactory = new FtpServerFactory();
           try {
			  for(BaseUser user:userList){
				  List<Authority> authorities = new ArrayList<Authority>();
				  authorities.add(new WritePermission());
				  user.setAuthorities(authorities);
				  serverFactory.getUserManager().save(user);
			  }
			 ListenerFactory listenerFactory = new ListenerFactory();
	         listenerFactory.setPort(port);
	         serverFactory.addListener("default", listenerFactory.createListener());
			 FtpServer server = serverFactory.createServer();
			 server.start();
			 log.info("ftp server start at port "+port);
		} catch (FtpException e) {
			e.printStackTrace();
		}
	} 

}
